<template>
	<view>
	<block v-if="cart.length !== 0">
		<!-- 收货地址 -->
		<my-address></my-address>
		<view  class="cart-container">
			<view class="cart-title">
				<uni-icons type="shop"></uni-icons>
				<text class="cart-title-text">购物车</text>
			</view>
			<uni-swipe-action>
			  <block v-for="(goods, i) in cart" :key="i">
			    <!-- uni-swipe-action-item 可以为其子节点提供滑动操作的效果。需要通过 options 属性来指定操作按钮的配置信息 -->
			    <uni-swipe-action-item :right-options="options"  @click="swipeitemClickHandler(goods)">
			      <my-good :goods="goods" :show-radio="true" :show-num="true"
				   @radio-change="radioChangeHandler" @num-Change="goodsNumChange"></my-good>
			    </uni-swipe-action-item>
			  </block>
			</uni-swipe-action>
		</view>
		<my-settle></my-settle>
	</block>

	
	<view v-if="cart.length === 0" class="emptyDataView">
		<image src="../../static/tab_icons/cart-active.png" mode=""></image>
		<text>暂无数据</text>
	</view>
	</view>

</template>

<script>
	import {mapState,mapGetters,mapMutations} from 'vuex'
	export default {
		data() {
			return {
				 options: [{
				      text: '删除', // 显示的文本内容
				      style: {
				        backgroundColor: '#C00000' // 按钮的背景颜色
				      }
				    }]
			};
		},
		computed:{
			...mapState('m_cart',['cart']),
			...mapGetters('m_cart',['total']),
			// ...mapMutations('m_cart',['updateGoodsState'])
		},
		watch: {
			total() {
				this.setBadge();
			}
		},
		onShow() {
			this.setBadge();
		},
		methods: {
			...mapMutations('m_cart',
			['updateGoodsState',
			'updateGoodsNumState',
			'removeGoodsById']),
	
			
			
					
			setBadge() {
				uni.setTabBarBadge({
					index:2,
					text:this.total + ''
				})
			},
			radioChangeHandler(e){
				this.updateGoodsState(e);
				// this.setBadge();
				
				
			},
			goodsNumChange(e) {
				console.log('商品数量发生变化')
				this.updateGoodsNumState(e)
				// this.setBadge();
			},
			// 点击了滑动操作按钮
			swipeitemClickHandler(goods) {
				console.log(goods)
				this.removeGoodsById(goods.goods_id);
				// this.setBadge();
				
			}
		}
	}
</script>

<style lang="scss">
	.emptyDataView {
		display: flex;
		align-items: center;
		flex-direction: column;
		height: 100%;
		width: 100%;
		justify-content: center;
		padding-top: 200px;
	
	}
.cart-title{
	display: flex;
	align-items: center;
	// margin: 10px;
	border-bottom: 1px solid #EBEEF5;
	padding: 10px;
	.cart-title-text {
		padding-left: 5px;
	}
}
.cart-container {
	padding-bottom: 50px;
}
</style>
